package com.iussoft.gateway.routes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apache.exception.BusinessException;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.apache.tools.UUIDUtil;
import com.iussoft.manager.RedisManager;
import com.iussoft.mysql.ReqParamVo;
import com.iussoft.mysql.UnifiedDataDao;
import com.iussoft.tools.JedisPoolFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: GateWay动态路由业务操作实现类
 * @author: Hou Dayu
 * @date: Created in 2020/2/25
 */
@Service("gatewayDefineService")
public class GatewayDefineService {

    /**
     * info_id,service_id,uri,predicates,limiter_rate,limiter_capacity,enabled,strip_prefix,route_order,version,weight,remarks
     */
    @Autowired
    private UnifiedDataDao unifiedDataDao;
    private final String tableName="dx_gw_process_control";//网关参数表
    private final String whiteTableName ="dx_gw_black_white";//黑白名单表

    public List<DataMap> gatewayDefineList(Map<String,String> param) throws BusinessException {
        ReqParamVo vo = new ReqParamVo();
        if(param.isEmpty()){
            param.put("enabled","1");
        }
        vo.setWheres(param);
        vo.setTableName(tableName);
        return unifiedDataDao.select(vo);
    }

    public List<DataMap> getWhiteList(Map<String,String> param) throws BusinessException {
        ReqParamVo vo = new ReqParamVo();
        vo.setWheres(param);
        vo.setTableName(whiteTableName);
        return unifiedDataDao.select(vo);
    }

    public boolean saveGatewayDefine(Map<String,String> param) throws BusinessException {
        if(StrUtil.isNull(param.get("infoId"))){
            param.put("infoId", UUIDUtil.generateId());
        }
        ReqParamVo vo = new ReqParamVo();
        vo.setWheres(param);
        vo.setTableName(tableName);
        boolean mark = unifiedDataDao.insertInfo(vo);
        return mark;
    }

    public boolean deleteGatewayDefine(String infoId) throws BusinessException {
        Map<String,String> param = new HashMap<>(1);
        param.put("infoId",infoId);
        ReqParamVo vo = new ReqParamVo();
        vo.setWheres(param);
        vo.setTableName(tableName);
        boolean mark = unifiedDataDao.delInfo(vo);
        return mark;
    }

    public boolean updateGatewayDefine(Map<String, String> param) throws BusinessException {
        ReqParamVo vo = new ReqParamVo();
        vo.setWheres("infoId",param.get("infoId"));
        param.remove("infoId");
        vo.setCloums(param);
        vo.setTableName(tableName);
        boolean mark = unifiedDataDao.updateInfo(vo);
        return mark;
    }

    public DataMap<String> gatewayDefineById(String infoId) throws BusinessException {
        Map<String, String> param = new HashMap<>();
        param.put("infoId",infoId);
        ReqParamVo vo = new ReqParamVo();
        vo.setWheres(param);
        vo.setTableName(tableName);
        return unifiedDataDao.selectById(vo);
    }
}
